'use strict';

/* eslint-disable */
$(function () {

  var page = 1
  var pageCount = 1
  var datas = []
  var itemData = null
  getEventList(page)

  function getEventList(p){
    $("section").remove(".new");
    $("li").remove(".pageicon");
    $.get('http://39.106.171.39:8011/mbl/Event/getEventList?pagesize=9&page='+ p, function(data, status){
      console.log('status:', status);
      console.log('data:', data);
      datas = data.rows;
      pageCount = data.pageCount
      let main = $(".main");
      if(datas.length==0){
        main.append('<p>暂无内容</p>')
      }else{
        for (let i = 0; i < datas.length; i++){
          if(i==0){
            let temp1 = $('<div class="item time t-a-c"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            let temp2 = $('<div class="desc"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            temp1.append(month).append(day)
            temp2.append(eventtitle).append(address)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==1){
            let temp1 = $('<div class="item time t-a-c"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            let temp2 = $('<div class="desc"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            temp1.append(month).append(day)
            temp2.append(eventtitle).append(address)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==2){
            let temp1 = $('<div class="time"></div>')
            let month = $('<p></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p></p>').text(dayFilter(datas[0].eventdate))
            let temp2 = $('<div class="desc"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            temp1.append(month).append(day)
            temp2.append(eventtitle).append(address)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==3){
            let temp1 = $('<div class="desc"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            let temp2 = $('<div class="time"></div>')
            let month = $('<p></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p></p>').text(dayFilter(datas[0].eventdate))
            temp1.append(eventtitle).append(address)
            temp2.append(month).append(day)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==4){
            let temp1 = $('<div class="time t-a-c"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            let temp2 = $('<div class="desc t-a-c"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            temp1.append(month).append(day)
            temp2.append(eventtitle).append(address)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==5){
            let temp1 = $('<div class="desc t-a-c"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            let temp2 = $('<div class="time t-a-c"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            temp1.append(eventtitle).append(address)
            temp2.append(month).append(day)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==6){
            let temp1 = $('<div class="desc t-a-c"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            let temp2 = $('<div class="time t-a-c f-r"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            temp1.append(eventtitle).append(address)
            temp2.append(month).append(day)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==7){
            let temp1 = $('<div class="time t-a-c"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            let temp2 = $('<div class="desc t-a-c"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            temp1.append(month).append(day)
            temp2.append(eventtitle).append(address)
            $('#e0').append(temp1).append(temp2)
          }
          if(i==8){
            let temp1 = $('<div class="time t-a-c"></div>')
            let month = $('<p class="month"></p>').text(monthFilter(datas[0].eventdate))
            let day = $('<p class="day"></p>').text(dayFilter(datas[0].eventdate))
            let temp2 = $('<div class="desc t-a-c"></div>')
            let eventtitle = $('<p></p>').text(datas[0].eventtitle)
            let address = $('<p></p>').text(datas[0].address)
            temp1.append(month).append(day)
            temp2.append(eventtitle).append(address)
            $('#e0').append(temp1).append(temp2)
          }
        }
        for(let i = datas.length; i<9; i++){
          $('#e'+i).hide()
        }
      }



      // 分页列表
      for (let i = 0; i < data.pageCount; i++ ) {
        let num = i + 1;
        let item = $("<li class='pageicon'></li>");
        let aElem = $('<span></span>').text(num);
        item.append(aElem);
        item.click(function (e) {
          // e.preventDefault()
          // window.location.href = window.location.pathname + "?page=" + num;
          getJournals(num)
        });
        item.insertBefore($("#next"));
      }
    });
  }

  function getEvent(id){

    $.get('http://39.106.171.39:8011/mbl/Event/getEventItem?id='+id, function(data, status){
      itemData = data
    })
  }

  $("#e0").click(function(){
    getEvent(datas[0].eventid)
  });
  $("#e1").click(function(){
    getEvent(datas[1].eventid)
  });
  $("#e2").click(function(){
    getEvent(datas[2].eventid)
  });
  $("#e3").click(function(){
    getEvent(datas[3].eventid)
  });
  $("#e4").click(function(){
    getEvent(datas[4].eventid)
  });
  $("#e5").click(function(){
    getEvent(datas[5].eventid)
  });
  $("#e6").click(function(){
    getEvent(datas[6].eventid)
  });
  $("#e7").click(function(){
    getEvent(datas[7].eventid)
  });
  $("#e8").click(function(){
    getEvent(datas[8].eventid)
  });






  // 分页
  var prev = $("#prev");
  prev.click(function () {
    // 获取路径参数
    if (page && Number(page) > 1) {
      page = Number(page) - 1;
      // window.location.href = window.location.pathname + "?page=" + page;
      if(now=='journals'){
        getJournals(page)
      }else if(now=='books'){
        getBooks(page)
      }else if(now=='patents'){
        getPatents(page)
      }
    }
  });
  var next = $("#next");
  next.click(function () {
    // 获取路径参数
    if (page<pageCount) {
      page = Number(page) + 1;
      // window.location.href = window.location.pathname + "?page="+page;
      if(now=='journals'){
        getJournals(page)
      }else if(now=='books'){
        getBooks(page)
      }else if(now=='patents'){
        getPatents(page)
      }
    }
  });
  function monthFilter(time) {
    if (!time) {
      return "";
    }
    const month = [
      "Jan.",
      "Feb.",
      "Mar.",
      "Apr.",
      "May",
      "June",
      "July",
      "Aug.",
      "Sept.",
      "Oct.",
      "Nov.",
      "Dec.",
    ];
    const monthStr = time.split("-")[1];
    const num = Number(monthStr);
    return month[num - 1];
  }
  function dayFilter(time) {
    if (!time) {
      return "";
    }
    return time.split("-")[2];
  }
  function timeEnFilter(time) {
    if (!time) {
      return "";
    }
    const month = [
      "Jan.",
      "Feb.",
      "Mar.",
      "Apr.",
      "May",
      "June",
      "July",
      "Aug.",
      "Sept.",
      "Oct.",
      "Nov.",
      "Dec.",
    ];
    const timeStr = time.split("-");
    const yearStr = timeStr[0];
    const monthStr = timeStr[1];
    const dayStr = timeStr[2];
    const num = Number(monthStr);
    return `${dayStr} ${month[num - 1]} ${yearStr}`;
  }


});
